/*
 * UserPageTitledEditHome.java
 *
 */

package net.kernub.client;

import com.google.gwt.core.client.GWT;

import com.google.gwt.i18n.client.Constants;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;

import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.DecoratorPanel;

import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ListBox;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.KeyCodes;

import net.kernub.client.core.RPC;
import net.kernub.client.core.RPCCallback;
import net.kernub.client.core.datatype.CString;
import net.kernub.client.core.datatype.CBoolean;

import net.kernub.client.PageTitled;

import net.kernub.client.text.RichTextToolbar;

public class UserPageTitledEditHome extends PageTitled
{
	private static String LANG_EN = "en";
	private static String LANG_FR = "fr";

	public interface Strings extends Constants
	{
		@DefaultStringValue( "Edit home text" )
		String title();

		@DefaultStringValue( "English" )
		String langEnglish();

		@DefaultStringValue( "French" )
		String langFrench();

		@DefaultStringValue( "Save" )
		String save();

		@DefaultStringValue( "Close" )
		String close();

		@DefaultStringValue( "Error while saving" )
		String errorSave();
	};

	private class EventHandler implements ClickHandler, ChangeHandler
	{
		public void onClick( ClickEvent event )
		{
			Widget sender = (Widget) event.getSource();

			if( sender == closeButton )
			{
				www.instance().setPage( www.PAGE_DISPLAY_LINKS );
			}
			else if( sender == saveButton )
			{
				save();
			}
		}

		// no use...
		public void onChange( ChangeEvent event )
		{
			Widget sender = (Widget) event.getSource();
		}
	};

	private final Strings constants = GWT.create( Strings.class );

	private final EventHandler eventHandler = new EventHandler();

	private final HorizontalPanel mainPanel = new HorizontalPanel();
	private final Button saveButton = new Button( constants.save(), eventHandler );
	private final Button closeButton = new Button( constants.close(), eventHandler );
	private final RichTextArea textArea = new RichTextArea();

	public UserPageTitledEditHome()
	{
		mainPanel.add( createButtonBar() );
		mainPanel.add( createRichTextArea() );

		mainPanel.setWidth( "100%" );
		mainPanel.setSpacing( 5 );
		setContent( mainPanel );
	}

	public void setActive()
	{
		super.setActive();

		this.setBigTitle( constants.title() );

		load();
	}

	private Widget createButtonBar()
	{
		closeButton.setWidth( "100%" );

		saveButton.setWidth( "100%" );
		
		final VerticalPanel panel = new VerticalPanel();
		panel.setSpacing( 15 );
		panel.add( saveButton );
		panel.add( closeButton );
		
		return panel;
	}

	private Widget createRichTextArea()
	{
		textArea.setSize( "100%", "20em" );

		final RichTextToolbar toolbar = new RichTextToolbar( textArea );
		toolbar.setWidth( "100%" );

		final VerticalPanel allPanel = new VerticalPanel();
		allPanel.setWidth( "100%" );
		allPanel.setSpacing( 0 );
		allPanel.add( toolbar );
		allPanel.add( textArea );

		final DecoratorPanel panel = new DecoratorPanel();
		panel.setWidth( "100%" );
		panel.setWidget( allPanel );

		return panel;
	}

	private void save()
	{
		closeButton.setEnabled( false );
		saveButton.setEnabled( false );

		RPC.call( "site/Home", "setText", textArea.getHTML(), new RPCCallback<CBoolean>() {
			public void onCallback( CBoolean r )
			{
				closeButton.setEnabled( true );
				saveButton.setEnabled( true );

				if( r.get() )
				{
					www.instance().setPage( www.PAGE_DISPLAY_LINKS );
				}
				else
				{
					UserPageTitledEditHome.this.setTempInfo( constants.errorSave() );
				}
			}
		});
	}

	private void load()
	{
		RPC.call( "site/Home", "getText", new RPCCallback<CString>() {
			public void onCallback( CString r )
			{
				textArea.setHTML( r.get() );
			}
		});
	}

};



